// example/sign/sign.js
var app = getApp()
import * as Api from '../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeColors: '',
    loginName: "",
    password: "",
    verifyInfo: '获取验证码',
    isdisable: false,
    contactMobile: '',
    index: 0,
    array: ['车主', '配件商', '修理厂'],
    code: '',//验证码
    objectArray: [
      {
        id: 0,
        name: '车主'
      },
      {
        id: 1,
        name: '配件商'
      },
      {
        id: 2,
        name: '修理厂'
      }
    ],
  },
  goLogin() {

    if (!this.data.hasGoneAway) {
      this.setData({ hasGoneAway: true })
      wx.redirectTo({
        url: '../login/login',
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }

  },
  vaild() {
    var that = this

    if (this.data.loginName.length!=11) {
      wx.showModal({
        title: '',
        content: '手机号格式错误',
        showCancel: false,
        cancelText: '',
        cancelColor: '',
        confirmText: '知道了',
        confirmColor: '',
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
      return;
    } else {

      if (that.data.isdisable == false) {
        wx.showLoading({
          title: '加载中',
          mask: true
        })
        that.getCode()

      }
    }


  },
  getCode() {

    let self = this
    let url = app.globalData.baseUrl1 + '/auth/getsmscode'
    let data = {
      mobile: this.data.loginName,


      type: 'SMS_118110004 '
    }
    let callBack = (res) => {

      if (res == null) {

        console.log("网络请求错误1！");
        return;
      }

      if (res.data.status != 200) {
        wx.showModal({
          title: '',
          content: res.data.data,
          showCancel: false,
          cancelText: '',
          cancelColor: '',
          confirmText: '知道了',
          confirmColor: '',
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
        return;
      }

      if (res.data.status == 200) {
        var count = 60;
        var timer = setInterval(function () {
          count--;
          if (count >= 1) {
            self.setData({
              verifyInfo: count + 's'
            })
          } else {
            self.setData({
              verifyInfo: '获取验证码'
            })
            clearInterval(timer);
            self.data.isdisable = false;
          }
        }, 1000);
        self.data.isdisable = true;
        wx.showToast({
          title: '发送成功',
          icon: 'success',
          duration: 2000
        })
      }
      wx.hideLoading()
    }
    Api.reqFormPost(url, data, callBack)

  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },

  sign() {
    if (!this.data.loginName.length > 0 || !this.data.password.length > 0 || !this.data.code.length > 0) {
      return
    }

    if (this.data.loginName.length!==11) {
      wx.showModal({
        title: "请输入正确的手机号"
      });
      return;
    } else {
      let self = this
      wx.getUserInfo({
        success: (res2) => {
          app.globalData.userInfo = res2.userInfo
          let url = app.globalData.baseUrl1 + '/user/password/find';
          let data = {
            mobile: this.data.loginName,
            password: this.data.password,
            code: this.data.code,
            merchantId: app.globalData.merchant.merchantId,
          }
          let callBack = (res) => {

            if (res == null) {

              console.log("网络请求错误！");
              return;
            }

            if (res.data.status != 200) {
              console.log('出错了', res)
              wx.showModal({
                title: '',
                content: res.data.message,
                showCancel: false,
                cancelText: '',
                cancelColor: '',
                confirmText: '知道了',
                confirmColor: '',
                success: function (res) {
                },
                fail: function (res) { },
                complete: function (res) { },
              })
              return
            }
            if (res.data.status === 200) {

              wx.redirectTo({
                url: '../login/login',
                success: function (res) {
                  wx.showToast({
                    title: '成功',
                    icon: 'success',
                    duration: 2000,
                    success() {
                    }
                  })
                },
                fail: function (res) { },
                complete: function (res) { },
              })


            }
          }
          Api.reqFormPost(url, data, callBack)
        }
      });
    }
  },
  changeInputCode(e) {
    this.setData({
      code: e.detail.value
    })
  },
  changeInputContactMobile(e) {
    this.setData({
      contactMobile: e.detail.value
    })
  },

  changeInputLoginName(e) {
    this.setData({
      loginName: e.detail.value
    })
  },
  changeInputPassword(e) {
    this.setData({
      password: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if (app.globalData.merchant === null) {
      app.doLogin(this.onLoad, this.onShow, options);
      return
    }
    this.setData({
      themeColors: app.globalData.merchant.themeColors
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})